package com.mls.fivedistrict.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.dao.IntegralMapper;
import com.mls.fivedistrict.dao.OrePoolMapper;
import com.mls.fivedistrict.pojo.*;
import com.mls.fivedistrict.server.IntegralService;
import com.mls.fivedistrict.server.PropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by nanji on 2018/3/20.
 */
@Service("integralService")
public class IntegralServiceImpl implements IntegralService {
    @Resource(name = "integralMapper")
    IntegralMapper integralMapper;
    @Resource(name = "orePoolMapper")
    OrePoolMapper orePoolMapper;

    @Autowired
    PropertyService propertyService;

    @Override
    public ServiceResponse addIntergral(Integral integral) {
        integral.setResidueDays(integral.getValidity());
        integral.setIsDelete(false);
        int row = integralMapper.insertSelective(integral);
        if (row > 0) {
            return ServiceResponse.createSuccessResponse("添加成功");
        }
        return ServiceResponse.createErrorResponse("添加失败");
    }

    @Override
    public ServiceResponse editIntegral(Integral integral) {
        int row = integralMapper.updateByPrimaryKeySelective(integral);
        if (row > 0) {
            return ServiceResponse.createSuccessResponse();
        }
        return ServiceResponse.createErrorResponse();
    }

    @Override
    public ServiceResponse getIntegral(Integral integral) {
        Integral data = integralMapper.selectByPrimaryKey(integral.getId());
        if (data != null) {
            return ServiceResponse.createSuccessResponse(data);
        }
        return ServiceResponse.createErrorResponse();
    }

    @Override
    public ServiceResponse getIntegralList(PageFilter pageFilter) {
        if (pageFilter.getPageNum() <= 0) {
            pageFilter.setPageNum(1);
        } else if (pageFilter.getPageSize() <= 0) {
            pageFilter.setPageSize(20);
        }
        IntegralExample integralExample = new IntegralExample();
        IntegralExample.Criteria criteria = integralExample.createCriteria();
        criteria.andIsDeleteEqualTo(false);
        integralExample.setOrderByClause("create_date desc");
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<Integral> integralList = integralMapper.selectByExample(integralExample);
        PageInfo<Integral> pageInfo = new PageInfo<Integral>(integralList);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    /**
     * 获取积分列表
     * 筛选条件是：未删除，状态为1（激活）
     * 返回值是积分列表
     *
     * @return
     */
    @Override
    public List<Integral> getIntegralListForTask() {
        IntegralExample integralExample = new IntegralExample();
        IntegralExample.Criteria criteria = integralExample.createCriteria();
        criteria.andStatusEqualTo("1");
        criteria.andIsDeleteEqualTo(false);
//        criteria.andResidueIntegralGreaterThan(new BigDecimal(0));
        List<Integral> integralList = integralMapper.selectByExample(integralExample);
        return integralList;
    }

    /**
     * @param id
     * @return
     */
    @Override
    public ServiceResponse getIntegral(Long id) {
        Integral integral = integralMapper.selectByPrimaryKey(id);
        if (integral != null) {
            return ServiceResponse.createSuccessResponse(integral);
        }
        return ServiceResponse.createErrorResponse("获取失败");
    }

    @Override
    public ServiceResponse deleteIntegral(Long id) {
        OrePoolExample orePoolExample = new OrePoolExample();
        OrePoolExample.Criteria criteria = orePoolExample.createCriteria();
        criteria.andIntrgralIdEqualTo(id);
        List<OrePool> orePoolList = orePoolMapper.selectByExample(orePoolExample);
        if (orePoolList == null || orePoolList.size() == 0) {
            return ServiceResponse.createErrorResponse("计算正在执行，无法删除！");
        }
        int row = integralMapper.softDelete(id);
        if (row > 0) {
            return ServiceResponse.createSuccessResponse();
        }
        return ServiceResponse.createErrorResponse();
    }

    @Override
    public Integral getIntegralDetail(Long id) {
        return integralMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Integral> getIntegralList() {
        List<Integral> integralList = integralMapper.selectByExample(null);
        return integralList;
    }

    @Override
    public List<Integral> getCanRewardIntegralList(PageFilter pageFilter, Long userId) {
        IntegralExample integralExample = new IntegralExample();
        IntegralExample.Criteria criteria = integralExample.createCriteria();
        criteria.andIsCanRewardsEqualTo(true);
        criteria.andIsDeleteEqualTo(false);
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<Integral> integralList = integralMapper.selectByExample(integralExample);
        return integralList;
    }

    @Override
    public List<Integral> getTransferIntegralList(PageFilter pageFilter, Long userId) {
        List<Property> properties = propertyService.getPropertiesByUserId(pageFilter, userId);
        List<Integral> integralList = new ArrayList<>();
        for (Property property : properties) {
            if (property.getTotal().compareTo(new BigDecimal("0")) > 0) {
                Integral integral = property.getIntegral();
                if (integral.getIsCanTransfer()) {
                    integral.setTotal(property.getTotal().setScale(4, BigDecimal.ROUND_DOWN));
                    integralList.add(integral);
                }

            }
        }
        return integralList;
    }

    @Override
    public int updateEdtegral(Integral integral) {
        int i = integralMapper.updateByPrimaryKeySelective(integral);
        return i;
    }
}
